package sk.tuke.constraintbuilder.views.components;

import javax.xml.bind.JAXBException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.ui.forms.widgets.FormToolkit;

import sk.tuke.constraintbuilder.editor.ConstraintEditorController;
import sk.tuke.constraintbuilder.editor.IConstraintController;
import sk.tuke.constraintbuilder.model.SimpleRuleElement;
import sk.tuke.constraintbuilder.utils.MyImages;
import sk.tuke.constraintbuilder.utils.MyUtils;

import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.widgets.ImageHyperlink;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.wb.swt.ResourceManager;

public class SimpleElementRulePanel extends Composite implements IRulePanel  {

	private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
	private SimpleRuleElement element;
	private boolean modified = false;
	private boolean isTemplate;
	
	//Controller
	private IConstraintController controller;
	
	//Components
	private Label descriptionLabel;
	private Text valueText;
	private Composite parent;

	public SimpleElementRulePanel(Composite parent, SimpleRuleElement element, IConstraintController controller, boolean isTemplate) {
		super(parent, SWT.BORDER);
		this.parent = parent;
		this.controller = controller;
		this.element = element;
		this.isTemplate = isTemplate;
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				toolkit.dispose();
			}
		});
		toolkit.adapt(this);
		toolkit.paintBordersFor(this);
		GridLayout gridLayout = new GridLayout(3, false);
		gridLayout.marginHeight = 1;
		setLayout(gridLayout);
		createControl();
		showRule();
	}
	
	private void createControl(){
		descriptionLabel = toolkit.createLabel(this, "Description", SWT.NONE);
		
		if(!isTemplate){
			valueText = new Text(this, SWT.BORDER);
			GridData gd_valueText = new GridData(SWT.LEFT, SWT.FILL, false, false, 1, 1);
			gd_valueText.heightHint = 10;
			valueText.setLayoutData(gd_valueText);
		}

		ImageHyperlink removeLink = toolkit.createImageHyperlink(this, SWT.NONE);
		removeLink.setImage(MyImages.REMOVE);
		toolkit.paintBordersFor(removeLink);
		removeLink.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				controller.removeRuleElement(element);
				dispose();
				adaptParents();
			}
		});
	}
	
	private void showRule(){
		String description;
		try {
			description = MyUtils.getConstraintPatterns().getDescription(element.getName());
		} catch (JAXBException e1) {
			description = element.getName();
		}
		if(description!=null)descriptionLabel.setText(description);
		if(!isTemplate){
			valueText.addModifyListener(new ModifyListener() {
				public void modifyText(ModifyEvent e) {
					modified = true;
				}
			});
			valueText.addListener(SWT.Verify, new Listener() {
				public void handleEvent(Event e) {
					String string = e.text;
					char[] chars = new char[string.length()];
					string.getChars(0, chars.length, chars, 0);
					for (int i = 0; i < chars.length; i++) {
						if (!('0' <= chars[i] && chars[i] <= '9')) {
							e.doit = false;
							return;
						}
					}
				}
		    });
			valueText.setText(String.valueOf(element.getValue()));
		}
		adaptParents();
	}
	
	private void adaptParents(){
		parent.layout();
		parent.getParent().layout();
		parent.getParent().getParent().layout();
	}
	
	public void doSave(){
		if(modified){
			element.setValue(Integer.parseInt(valueText.getText()));
		}
	}

}
